package com.dys.designpattern.factorymethod.applyaward.controller;

import com.dys.designpattern.factorymethod.applyaward.entity.AwardInfo;
import com.dys.designpattern.factorymethod.applyaward.factory.FreeGoodsFactory;
import com.dys.designpattern.factorymethod.applyaward.factory.FreeGoodsFactoryMap;
import com.dys.designpattern.factorymethod.applyaward.factory.impl.DiscountFreeGoodsFactory;
import com.dys.designpattern.factorymethod.applyaward.factory.impl.SmallGiftFreeGoodsFactory;
import com.dys.designpattern.factorymethod.applyaward.factory.impl.YouKuMemberFreeGoodsFactory;
import com.dys.designpattern.factorymethod.applyaward.service.IFreeGoods;
import com.dys.designpattern.factorymethod.applyaward.vo.ResponseResult;

/**
 * @Author: 疾风
 * @ClassName: DeliverController
 * @Description: 按照类型的不同发放商品
 * @CreateDate: 2024/05/14
 * @Version: v1.0.0
 */
public class DeliverController {
    public ResponseResult awardToUser(AwardInfo awardInfo) {
        // 从上面的代码实现来看，工厂类对象的创建逻辑又耦合进了 awardToUser() 方法中，
        // 跟我们最初的代码版本非常相似,引入工厂方法非但没有解决问题，反倒让设计变得更加复杂了。
        FreeGoodsFactory freeGoodsFactory = null;
        if (awardInfo.getAwardType() == 1) {
            freeGoodsFactory = new DiscountFreeGoodsFactory();
        } else if (awardInfo.getAwardType() == 2) {
            freeGoodsFactory = new YouKuMemberFreeGoodsFactory();
        } else {
            freeGoodsFactory = new SmallGiftFreeGoodsFactory();
        }

        IFreeGoods instance = freeGoodsFactory.getInstance();
        System.out.println("=====工厂方法模式========");
        ResponseResult responseResult = instance.sendFreeGoods(awardInfo);
        return responseResult;
    }

    public ResponseResult awardToUser2(AwardInfo awardInfo) {

        // 根据类型获取工厂
        FreeGoodsFactory goodsFactory = FreeGoodsFactoryMap.getParserFactory(awardInfo.getAwardType());

        // 从工厂中获取对应实例
        IFreeGoods freeGoods = goodsFactory.getInstance();
        ResponseResult result = freeGoods.sendFreeGoods(awardInfo);
        return result;
    }
}
